#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct ListNode 
{
	int val;
	struct ListNode* next;	
};
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
    struct ListNode* tailA = headA;
    struct ListNode* tailB = headB;
    int lenA = 1;
    int lenB = 1;
    while (tailA->next)
    {
        lenA++;
        tailA = tailA->next;
    }
    while (tailB->next)
    {
        lenB++;
        tailB = tailB->next;
    }
    int gap = abs(lenA - lenB);
    struct ListNode* longList = headA;
    struct ListNode* shortList = headB;
    if (lenA < lenB)
    {
        longList = headB;
        shortList = headA;
    }
    while (gap--)
    {
        longList = longList->next;
    }
    while (longList && shortList)
    {
        if (longList == shortList)
            return longList;
        longList = longList->next;
        shortList = shortList->next;
    }
    return NULL;
}
